﻿using System.IO;
using Greg.XmlEditor.Model;

namespace Greg.XmlEditor.Business.Concrete
{
	public class DocumentService : IDocumentService
	{
		public Document Create()
		{
			return new Document();
		}

		public void Load(DocumentBase document, string fileName)
		{
			var fi = new FileInfo(fileName);
			if (!fi.Exists)
				throw new ValidationException("The file \"{0}\" doesn't exists.", fileName);

			var text = File.ReadAllText(fi.FullName);

			document.FullName = fileName;
			document.Text = text;
			document.IsDirty = false;
		}

		public void Save(DocumentBase document, string fileName)
		{
			File.WriteAllText(fileName, document.Text);
			document.FullName = fileName;
			document.IsDirty = false;
		}
	}
}